package com.jf.blogbackened.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    public  static Long StringToTimeStamp(String date) throws ParseException {
        Date parse = parse(date);
        Calendar cal = Calendar.getInstance();
        cal.setTime(parse);
        return cal.getTimeInMillis();
    }
    /**
     * 获取当前时间戳
     * 格式为：Unix时间戳
     */
    public static long getUnixTimeStamp(long time) {
        return time / 1000;
    }

    /**
     * Unix时间戳转时间戳
     * @param unixTimeStamp
     * @return
     */
    public static long toTimestamp(long unixTimeStamp) {
        return unixTimeStamp * 100;
    }
    /**
     * 获得该月第一天
     * 格式："yyyy年MM月dd日"
     * @param date 格式："yyyy年MM月"
     * @return 格式："yyyy年MM月dd日"
     */
    public static Long getFirstDayOfMonth(String date) throws ParseException {
        Calendar cal = Calendar.getInstance();
        cal.setTime(parse(date));
        //获取某月最小天数
        int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        return getUnixTimeStamp(cal.getTimeInMillis());
    }

    public static Date parse(String date) throws ParseException {
        return new SimpleDateFormat("yyyy-MM-dd").parse(date);
    }

    public static String format(Date date) throws ParseException {
        return new SimpleDateFormat("yyyy年MM月dd日").format(date);
    }

    /**
     * 获得该月最后一天时间戳
     * @param date 格式："yyyy年MM月"
     * @return 格式："yyyy年MM月dd日"
     */
    public static Long getLastDayOfMonth(String date) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月");
        Date parse = format.parse(date);
        Calendar cal = Calendar.getInstance();
        cal.setTime(parse);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //return format(cal.getTime());
        return getUnixTimeStamp(cal.getTimeInMillis());
    }
    public static String parseTimeStrap(Long timeStrap) throws ParseException {
        return new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss").format(new Date(timeStrap));
    }
}
